package com.spsoft.vatoutinv.service.official.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.crypto.symmetric.SymmetricCrypto;
import com.spsoft.core.model.Result;
import com.spsoft.system.model.vo.official.InvoiceDownloadVO;
import com.spsoft.system.model.vo.official.SdpXzdzVO;

import java.util.List;
import java.util.stream.Collectors;

public class OfficialServiceCommonMtd {
    /**
     * 拼接电子发票下载地址
     * @param dzfpUrlPrefix
     * @param fpdm
     * @param fphm
     * @param nssbh
     * @return
     */
    public static String getDzfpUrl(String dzfpUrlPrefix, String fpdm, String fphm, String nssbh) {
        return dzfpUrlPrefix +"?invoiceNumber=" + fphm + "&invoiceCode=" + fpdm + "&taxnum=" + nssbh;
    }

    /**
     * 数电票下载链接去重
     * @param downloadVO
     * @return
     */
    public static Result<InvoiceDownloadVO> uniqueSdpList(InvoiceDownloadVO downloadVO) {
        if (CollectionUtil.isEmpty(downloadVO.getSdpXzdzList())) {
            return Result.OK(downloadVO);
        }
        List<SdpXzdzVO> unique = downloadVO.getSdpXzdzList().stream().distinct().collect(Collectors.toList());
        downloadVO.setSdpXzdzList(unique);
        return Result.OK(downloadVO);
    }

    public static SdpXzdzVO getSdpXzdzVO(String fphm, String urlPrefix, byte[]  sm4KeyBytes) {
//        String urlPrefix = interfacesProperties.getSdpDownloadUrlPrefix();
        // http://localhost:8080/vmsFullOutInvoice/singleInvoiceDownload?text=加密字符串&type=pdf
//        byte[] bytes = sm4Properties.getKey().getBytes();
        SymmetricCrypto sm4 = new SymmetricCrypto("SM4/ECB/PKCS5Padding", sm4KeyBytes);
        String encryptHex = sm4.encryptHex(fphm);
        String ofd = urlPrefix + encryptHex + "&type=ofd";
        String pdf = urlPrefix + encryptHex + "&type=pdf";
        String xml = urlPrefix + encryptHex + "&type=xml";
        SdpXzdzVO dz = new SdpXzdzVO();
        dz.setSdpXzdzOfd(ofd);
        dz.setSdpXzdzXml(xml);
        dz.setSdpXzdzPdf(pdf);
        return dz;
    }
}
